<template>
	<view style="min-height: 100vh;background: #F3F3F3;">
		
		<view class="wrap">
			<view class="storeItem" v-for="(item,index) in list" :key="index" @click="changeSelect" :data-index="index" v-if="list.length>0">
				<view :class="['choiceWrap',selectArr[index]?'active':'']">
					<image src="../../static/mine/right_white.png" v-if="selectArr[index]"></image>
				</view>
				<image :src="getImage(item.image_url)"></image>
				<view class="storeItemR">
					<view>{{item.name}}</view>
					<view>{{item.industry?item.industry.name:"无"}}</view>
					<view>{{item.detailed_address?item.detailed_address:"无"}}</view>
				</view>
			</view>
			<view class="addStore" @click="addStore" v-if="!userId&&type!='auth'">
				<image src="../../static/mine/addStore.png"></image>
				<view>新增门店</view>
			</view>
		</view>
		<view v-if="list.length==0" style="padding-top: 200upx;">
			<u-empty></u-empty>
		</view>
		<view class="sureBtn" @click="submit">确认</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				selectArr:[],
				list:[],
				page:1,
				id:"",
				moreFlag:false,
				type:"common",
				selectList:[],
				userId:"",
				oid:"",
				reqFlag:true,
				choiceArr:[]
			}
		},
		onShow() {
			if(this.id||this.userId){
				this.getList();
			}
		},
		onLoad(options) {
			this.id = options.id;
			if(options.type){
				this.type = options.type;
				if(this.type=='auth'||this.type=='authCp'){
					this.choiceArr = uni.getStorageSync("storeData");
					uni.setNavigationBarTitle({
						title:"请选择对应类目且已完成认证的门店"
					})
				}else{
					uni.setNavigationBarTitle({
						title:"选择门店"
					})
				}
			}
			if(options.userid){
				this.userId = options.userid;
			}
			if(options.oid){
				this.oid = options.oid;
			}
			this.getList();
		},
		onReachBottom() {
			if(!this.moreFlag){
				return false;
			}
			this.moreFlag = false;
			this.page = this.page + 1;
			this.getList();
		},
		methods: {
			submit(){
				if(!this.reqFlag){
					return false;
				}
				if(this.userId){
					// console.log(this.selectArr[0])
					let shopId = null;
					for(let i=0;i<this.selectArr.length;i++){
						if(this.selectArr[i]){
							// arr.push(this.list[i]);
							shopId = this.list[i].id;
						}
					}
          // console.log(shopId)
					if(!shopId){
						this.$refs.uToast.show({
							title:"请选择门店",
							type:"error",
							duration:2000
						})
						return false;
					}
					if(this.oid){
						this.reqFlag = false;
						this.$u.api.shopPackageOrderWriteOff({order_number:this.oid,shop_id:shopId}).then(rep=>{
							console.log(rep)
							this.reqFlag = true;
							let pages = getCurrentPages();
							pages[pages.length-2].$vm.$refs.uToast.show({
								title:"核销成功",
								type:"success",
								duration:2000
							})
							uni.navigateBack({
								delta:1
							})
						})
						.catch(err=>{
							this.reqFlag = true;
						})
					}else{
						uni.$emit("confirmTime",shopId);
						uni.navigateBack({
							delta:1
						})
					}
				}else{
					let arr = [];
					for(let i=0;i<this.selectArr.length;i++){
						if(this.selectArr[i]){
							arr.push(this.list[i]);
						}
					}
					let page = getCurrentPages();
					page[page.length-2].$vm.storeData = arr;
					console.log(arr)
					this.selectList = arr;
					let funArr = [];
					for(let i=0;i<arr.length;i++){
						funArr.push(this.getCityMoney(arr[i].city_id))
					}
					Promise.all(funArr).then(res=>{
						//console.log(res)
						if(this.type=='auth'){
							let obj = {};
							obj.group_buying_percentage = Number(res[0].data.group_buying_percentage);
							obj.group_procedures_percentage = Number(res[0].data.group_procedures_percentage);
							obj.id = 1;
							for(let i=0;i<res.length;i++){
								if(res[i].data.group_buying_percentage > obj.group_buying_percentage){
									obj.group_buying_percentage = res[i].data.group_buying_percentage;
								}
								if(res[i].data.group_procedures_percentage > obj.group_procedures_percentage){
									obj.group_procedures_percentage = res[i].data.group_procedures_percentage;
								}
							}
							page[page.length-2].$vm.moneyObj = obj;
							if(page[page.length-2].$vm.formData.money){
								if(page[page.length-2].$vm.formData.user_commission_money){
									if(Number((Number(page[page.length-2].$vm.formData.money)*Number(obj.group_buying_percentage)/1000).toFixed(2))>Number(page[page.length-2].$vm.formData.user_commission_money)){
										page[page.length-2].$vm.formData.user_commission_money = Number((Number(page[page.length-2].$vm.formData.money)*Number(obj.group_buying_percentage)/1000).toFixed(2));
										page[page.length-2].$vm.$refs.uToast.show({
											title:"最低分享佣金为"+page[page.length-2].$vm.formData.user_commission_money+"元",
											type:"error",
											duration:2000
										})
									}
								}else{
									page[page.length-2].$vm.formData.user_commission_money = Number((Number(page[page.length-2].$vm.formData.money)*Number(obj.group_buying_percentage)/1000).toFixed(2));
									page[page.length-2].$vm.$refs.uToast.show({
										title:"已为您适配分享佣金,请留意",
										type:"success",
										duration:2000
									})
								}
							}
						}else if(this.type=='authCp'){
							let obj = {};
							obj.evaluation_money = Number(res[0].data.evaluation_money);
							obj.evaluation_percentage = Number(res[0].data.evaluation_percentage);
							obj.evaluation_procedures_money = Number(res[0].data.evaluation_procedures_money);
							obj.evaluation_procedures_percentage = Number(res[0].data.evaluation_procedures_percentage);
							obj.id = 1;
							for(let i=0;i<res.length;i++){
								if(res[i].data.evaluation_money > obj.evaluation_money){
									obj.evaluation_money = res[i].data.evaluation_money;
								}
								if(res[i].data.evaluation_percentage > obj.evaluation_percentage){
									obj.evaluation_percentage = res[i].data.evaluation_percentage;
								}
								if(res[i].data.evaluation_procedures_money > obj.evaluation_procedures_money){
									obj.evaluation_procedures_money = res[i].data.evaluation_procedures_money;
								}
								if(res[i].data.evaluation_procedures_percentage > obj.evaluation_procedures_percentage){
									obj.evaluation_procedures_percentage = res[i].data.evaluation_procedures_percentage;
								}
							}
							page[page.length-2].$vm.moneyObj = obj;
							if(page[page.length-2].$vm.formData.talent_money){
								if(page[page.length-2].$vm.moneyBl){
									if(Number(page[page.length-2].$vm.moneyBl)*10<obj.evaluation_percentage){
										let newMoney = Number(page[page.length-2].$vm.formData.talent_money)*Number(obj.evaluation_percentage)/1000;
										if(newMoney<obj.evaluation_money){
											newMoney = obj.evaluation_money;
											page[page.length-2].$vm.moneyBl = Number(obj.evaluation_money)/Number(page[page.length-2].$vm.formData.talent_money)*100;
											page[page.length-2].$vm.formData.user_commission_money = obj.evaluation_money;
											page[page.length-2].$vm.$refs.uToast.show({
												title:"最低分享佣金为"+page[page.length-2].$vm.formData.user_commission_money+"元",
												type:"error",
												duration:2000
											})
										}else{
											page[page.length-2].$vm.moneyBl = Number(obj.evaluation_percentage)/10;
											page[page.length-2].$vm.formData.user_commission_money = newMoney;
											page[page.length-2].$vm.$refs.uToast.show({
												title:"最低分享佣金为"+page[page.length-2].$vm.formData.user_commission_money+"元",
												type:"error",
												duration:2000
											})
										}
									}else{
										// let newMoney = Number(page[page.length-2].$vm.formData.user_commission_money)*Number(obj.evaluation_percentage)/1000;
										if(Number(page[page.length-2].$vm.formData.user_commission_money)<obj.evaluation_money){
											page[page.length-2].$vm.formData.user_commission_money = obj.evaluation_money;
											page[page.length-2].$vm.moneyBl = Number(obj.evaluation_money)/Number(page[page.length-2].$vm.formData.talent_money)*100;
											page[page.length-2].$vm.$refs.uToast.show({
												title:"最低分享佣金为"+page[page.length-2].$vm.formData.user_commission_money+"元",
												type:"error",
												duration:2000
											})
										}
									}
								}else{
									let newMoney = Number(page[page.length-2].$vm.formData.talent_money)*Number(obj.evaluation_percentage)/1000;
									if(newMoney<obj.evaluation_money){
										newMoney = obj.evaluation_money;
										page[page.length-2].$vm.moneyBl = Number(obj.evaluation_money)/Number(page[page.length-2].$vm.formData.talent_money)*100;
										page[page.length-2].$vm.formData.user_commission_money = obj.evaluation_money;
										page[page.length-2].$vm.$refs.uToast.show({
											title:"最低分享佣金为"+page[page.length-2].$vm.formData.user_commission_money+"元",
											type:"error",
											duration:2000
										})
									}else{
										page[page.length-2].$vm.moneyBl = Number(obj.evaluation_percentage)/10;
										page[page.length-2].$vm.formData.user_commission_money = newMoney;
										page[page.length-2].$vm.$refs.uToast.show({
											title:"最低分享佣金为"+page[page.length-2].$vm.formData.user_commission_money+"元",
											type:"error",
											duration:2000
										})
									}
								}
							}
						}
						uni.navigateBack({
							delta:1
						})
					})
				}
			},
			getCityMoney(id){
				return new Promise((resolve,reject)=>{
					this.$u.api.areaDetails({id:id}).then(res=>{
						resolve(res)
					})
				})
			},
			addStore(){
				uni.navigateTo({
					url:"/pages/pagetwo/pages/CreateStore/CreateStore"
				})
			},
			getImage(url){
				if(url&&(url.indexOf("http://")!=-1||url.indexOf("https://")!=-1)){
					return url;
				}else{
					return app.$vm.globalData.imgBaseUrl + url;
				}
			},
			changeSelect(e){
				console.log(e)
				let index = e.target.dataset.index || e.currentTarget.dataset.index;
				let arr = this.selectArr;
				let flag = !arr[index];
				if(this.userId){
					for(let i=0;i<arr.length;i++){
						arr.splice(i,1,false);
					}
					arr.splice(index,1,true);
				}else{
					arr.splice(index,1,flag);
				}
				this.selectArr = arr;
			},
			getList(){
				let obj = {};
				// //obj.data = {};
				obj.page = this.page;
				if(this.userId){
					if(this.oid){
						this.$u.api.shopPackageDetails({shop_package_id:this.userId}).then(res=>{
							//console.log(res)
							this.selectArr = [];
							for(let i=0;i<res.data.details.ShopPackageRelation.length;i++){
								this.selectArr.push(false);
							}
							this.list = res.data.details.ShopPackageRelation.map(cur=>{
								return cur.Shop;
							});
						})
					}else{
						obj.shop_evaluation_id = this.userId;
						obj.longitude = uni.getStorageSync("longitude");//117.082094;;
						obj.dimension = uni.getStorageSync("latitude");//39.987725;
						this.$u.api.shopEvaluationRelationList(obj).then(res=>{
							if(this.page == 1){
								this.selectArr = [];
								for(let i=0;i<res.list.data.length;i++){
									this.selectArr.push(false);
								}
								this.list = res.list.data.map(cur=>{
									return cur.Shop;
								});
							}else{
								for(let i=0;i<res.list.data.length;i++){
									this.selectArr.push(false);
								}
								this.list = this.list.concat(res.list.data.map(cur=>{
									return cur.Shop;
								}));
							}
							if(this.page<res.list.total){
								this.moreFlag = true;
							}else{
								this.moreFlag = false;
							}
						})
					}
					
				}else{
					obj.industry_id = this.id;
					if(this.type=="auth"||this.type=='authCp'){
						obj.is_authentication = 1;
						obj.user_id = uni.getStorageSync("userId");
					}
					obj.lng = uni.getStorageSync("longitude");//117.082094;
					obj.lat = uni.getStorageSync("latitude");//39.987725;
					this.$u.api.shopList(obj).then(res=>{
					if(res.code==200) {
            if (this.page == 1) {
              this.selectArr = [];
              for (let i = 0; i < res.data.length; i++) {
                this.selectArr.push(false);
              }
              if (this.choiceArr.length > 0) {
                for (let i = 0; i < res.data.length; i++) {
                  for (let j = 0; j < this.choiceArr.length; j++) {
                    if (this.choiceArr[j].id == res.data[i].id) {
                      this.selectArr.splice(i, 1, true);
                    }
                  }
                }
              }
              this.list = res.data;
            } else {
              for (let i = 0; i < res.data.length; i++) {
                this.selectArr.push(false);
              }
              if (this.choiceArr.length > 0) {
                for (let i = 0; i < res.data.length; i++) {
                  for (let j = 0; j < this.choiceArr.length; j++) {
                    if (this.choiceArr[j].id == res.data[i].id) {
                      this.selectArr.splice(i, 1, true);
                    }
                  }
                }
              }
              this.list = this.list.concat(res.data);
            }
            if (this.page < res.data.last_page) {
              this.moreFlag = true;
            } else {
              this.moreFlag = false;
            }
          }
					})
				}
				
				
			}
		}
	}
</script>

<style scoped>
	
	
	
	.sureBtn{
		width: 100%;
		height: 100upx;
		line-height: 100upx;
		text-align: center;
		color: white;
		font-size: 28upx;
		background: #FFAC3F;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 9;
	}
	
	
	.addStore{
		width: 100%;
		height: 72upx;
		background: white;
		border-radius: 8upx;
		border: 2upx dashed #FFAC3F;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.addStore>image{
		width: 24upx;
		height: 24upx;
		margin-right: 14upx;
	}
	.addStore>view{
		font-size: 24upx;
		line-height: 24upx;
		color: #FFAC3F;
	}
	
	
	.wrap{
		padding: 16upx 24upx 124upx;
	}
	.storeItem{
		width: 100%;
		background: white;
		border-radius: 8upx;
		padding: 24upx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		margin-bottom: 16upx;
	}
	.storeItem:last-child{
		margin-bottom: 0;
	}
	.choiceWrap{
		width: 24upx;
		height: 24upx;
		box-sizing: border-box;
		border: 2upx solid #BFBFBF;
		border-radius: 4upx;
		margin-right: 24upx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.choiceWrap.active{
		border-color: #FFAC3F;
		background: #FFAC3F;
	}
	.choiceWrap>image{
		width: 12upx;
		height: 12upx;
	}
	.storeItem>image{
		width: 96upx;
		height: 96upx;
		border-radius: 8upx;
		margin-right: 16upx;
	}
	.storeItemR{
		width: calc(100% - 160upx);
		height: 96upx;
		box-sizing: border-box;
		padding-top: 8upx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}
	.storeItemR>view{
		width: 100%;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	.storeItemR>view:nth-child(1){
		font-size: 20upx;
		line-height: 20upx;
		color: #333333;
	}
	.storeItemR>view:nth-child(2){
		font-size: 20upx;
		line-height: 20upx;
		color: #999999;
	}
	.storeItemR>view:nth-child(3){
		font-size: 16upx;
		line-height: 16upx;
		color: #999999;
	}
</style>
